//
//  ChoseIndustryView.m
//  mendianjieke
//
//  Created by 响当当 on 11/12/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "ChoseIndustryView.h"

@interface ChoseIndustryView()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    NSInteger _yearIndex;
    
    NSInteger _monthIndex;
}
@property (nonatomic, strong)UIPickerView *industryPickeView;
@property (nonatomic, strong)UIButton *gotoKwayButton;
@property (nonatomic, copy) NSArray *yearArray;
@property (nonatomic, strong)NSString *type;
@property (nonatomic, strong)HWTabBarController *vc;
@property (nonatomic, strong)UILabel *titleLabel;//demo版本
@end

@implementation ChoseIndustryView

- (NSArray *)yearArray {
    
    if (_yearArray == nil) {
        
        _yearArray = [[NSArray alloc]init];
    }
    
    return _yearArray;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [Utilties creatLabelWithFrame:CGRectMake(22.5*kUIScaleBasePlus,170*kUIScaleBasePlus,SCREEN_WIDTH-45*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                       textAligment:NSTextAlignmentCenter
                                           textClor:LCHexColor(0x000000)
                                           textFont:14*kUIScaleBasePlus];
    }
    return _titleLabel;
}

- (UIPickerView *)industryPickeView
{
    if (!_industryPickeView)
    {
        UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,200*kUIScaleBasePlus)];
        bgImageView.image = [UIImage imageNamed:@"industry_bg"];
        [self addSubview:bgImageView];
        
        UILabel *titleLabel = [Utilties creatLabelWithFrame:CGRectMake(27.5*kUIScaleBasePlus,19*kUIScaleBasePlus,120*kUIScaleBasePlus,18.5*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentLeft
                                                   textClor:LCHexColor(0xFFFFFF)
                                                   textFont:15*kUIScaleBasePlus];
        titleLabel.text = @"请选择";
        [self addSubview:titleLabel];
        
        UILabel *subTitleLabel = [Utilties creatLabelWithFrame:CGRectMake(27.5*kUIScaleBasePlus,CGRectGetMaxY(titleLabel.frame)+5*kUIScaleBasePlus,160*kUIScaleBasePlus,25.5*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentLeft
                                                   textClor:LCHexColor(0xFFFFFF)
                                                   textFont:20*kUIScaleBasePlus];
        subTitleLabel.text = @"您的具体行业";
        [self addSubview:subTitleLabel];
        
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(22.5*kUIScaleBasePlus,150*kUIScaleBasePlus,SCREEN_WIDTH-45*kUIScaleBasePlus, 427*kUIScaleBasePlus)];
        bgView.backgroundColor = [UIColor whiteColor];
        bgView.layer.masksToBounds = YES;
        bgView.layer.cornerRadius = 15*kUIScaleBasePlus;
        [self addSubview:bgView];
        
        _industryPickeView = [[UIPickerView alloc]initWithFrame:CGRectMake(65.5*kUIScaleBasePlus,200*kUIScaleBasePlus,244*kUIScaleBasePlus, 300*kUIScaleBasePlus)];
        _industryPickeView.delegate = self;
        _industryPickeView.dataSource =self;
        _industryPickeView.backgroundColor = [UIColor whiteColor];
        
        UIButton *completeBtn = [Utilties creatButtonWithFrame:CGRectMake(60.5*kUIScaleBasePlus,CGRectGetMaxY(_industryPickeView.frame)+10*kUIScaleBasePlus,254*kUIScaleBasePlus,44*kUIScaleBasePlus)
                                                  textAligment:UIControlContentHorizontalAlignmentCenter
                                                          text:@"完成"
                                                      textClor:LCHexColor(0xFFFFFF)
                                                     buttonBGC:JSAMainColor
                                                      textFont:22*kUIScaleBasePlus
                                                        target:self
                                                        method:@selector(complete)];
        completeBtn.layer.masksToBounds = YES;
        completeBtn.layer.cornerRadius = completeBtn.frame.size.height/2;
        [self addSubview:completeBtn];
        
    }
    return _industryPickeView;
}

- (UIButton *)gotoKwayButton
{
    if (!_gotoKwayButton)
    {
        _gotoKwayButton = [Utilties                         creatButtonWithFrame:CGRectMake(0,SCREEN_HEIGHT-25.5*kUIScaleBasePlus-13*kUIScaleBasePlus,SCREEN_WIDTH,13*kUIScaleBasePlus)
        textAligment:UIControlContentHorizontalAlignmentCenter
                text:@"开启您的KWAY之旅"
            textClor:LCHexColor(0x939393)
           buttonBGC:[UIColor clearColor]
            textFont:14*kUIScaleBasePlus
              target:self
              method:@selector(gotoKway)];
    }
    return _gotoKwayButton;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _yearIndex = 0;
        self.backgroundColor = LCHexColor(0xF8F8F8);
        [self addSubview:self.industryPickeView];
        [self addSubview:self.gotoKwayButton];
        [self addSubview:self.titleLabel];
        [self.industryPickeView selectRow:_yearIndex inComponent:0 animated:YES];

    }
    return self;
}

#pragma mark - delegate & datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.yearArray.count;
}

#pragma mark -UIPickerView的代理

// 滚动UIPickerView就会调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _yearIndex = row;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        UILabel *label = (UILabel *)[pickerView viewForRow:row forComponent:component];
        label.textColor = JSAMainColor;
        label.font = [UIFont systemFontOfSize:20*kUIScaleBasePlus];
        
    });
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    
    //设置文字的属性
    UILabel *genderLabel = [[UILabel alloc] init];
    genderLabel.textAlignment = NSTextAlignmentCenter;
    genderLabel.textColor = LCHexColor(0x9E9E9E);
    genderLabel.font = [UIFont systemFontOfSize:16*kUIScaleBasePlus];
    IndustryModel *model = [[IndustryModel alloc]initWithDic:self.yearArray[row]];
    genderLabel.text = model.name;
    
    return genderLabel;
}

#pragma mark - 选择行业
- (void)complete
{
     IndustryModel *model = [[IndustryModel alloc]initWithDic:self.yearArray[_yearIndex]];
     NSDictionary *pramaters = @{
           @"industryId":model.num
                                      };

       WeakSelf(self)
          [infoManager completeIndustryInfo:pramaters
                            sucess:^(id  _Nonnull acceptData) {
              NSNumber *num = acceptData;
              if (num.integerValue == 201)
              {
                  [[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshLabel" object:nil userInfo:nil];
                                                           
                   if ([self.type isEqualToString:@"0"])
                   {
                       UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
                           while (topRootViewController.presentedViewController)
                           {
                               topRootViewController = topRootViewController.presentedViewController;
                           }
                           
                           [topRootViewController dismissViewControllerAnimated:YES completion:^{
                               
                           }];
                       }
                       else
                       {
                           weakSelf.vc = [[HWTabBarController alloc]init];
                           
                           AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                           
                           kKeyWindow.rootViewController = appDelegate.vc;
                           [kKeyWindow.window makeKeyAndVisible];
                       }

                       NSString *str = current_User.detail;
                       if (![str isEqualToString:@""])
                       {
                           NSDictionary *dic = @{@"detail":str,@"isNew":@"0"};
                           [[NSNotificationCenter defaultCenter] postNotificationName:@"UPDATEVERSON" object:nil userInfo:dic];
                                                               
                       }
                   }
              }
                              fail:^(NSError * _Nullable error) {
                                  
                              }];
}

- (void)gotoKway
{
    
}

#pragma mark - 获取行业数据
- (void)getDate:(NSArray *)arr
{
    
    if (current_User.isDemoUser.integerValue == 0)
    {
        self.titleLabel.text = @"";
    }
    else
    {
        self.titleLabel.text = @"导购天眼支持绝大部分行业";
    }
    self.yearArray = [NSArray arrayWithArray:arr];
    [self.industryPickeView reloadAllComponents];
    _yearIndex = self.yearArray.count/2;
    [self.industryPickeView selectRow:_yearIndex inComponent:0 animated:YES];
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           
         UILabel *label = (UILabel *)[self.industryPickeView viewForRow:self->_yearIndex forComponent:0];
           label.textColor = JSAMainColor;
           label.font = [UIFont systemFontOfSize:20*kUIScaleBasePlus];
           
       });
}

- (void)getType:(NSString *)type
{
    self.type = type;
}
@end
